<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import WelcomeDiv from '@/views/components/WelcomeDiv.vue'
import { billCarInService, billCarOutService } from '@/api/bill'

const stream = ref()

const inVideoRef = ref(null)
const inImage = ref(null)

const outVideoRef = ref(null)
const outImage = ref(null)

const getCameraStream = async () => {
  try {
    stream.value = await navigator.mediaDevices.getUserMedia({ video: true })
    // 将视频流设置到video元素的srcObject上
    inVideoRef.value.srcObject = stream.value
    outVideoRef.value.srcObject = stream.value
  } catch (err) {
    ElMessage.error('Error accessing camera:', err)
  }
}

const captureInImage = () => {
  if (inVideoRef.value && inVideoRef.value.currentTime > 0) {
    // 使用canvas捕获当前视频帧
    const canvas = document.createElement('canvas')
    const context = canvas.getContext('2d')
    canvas.width = inVideoRef.value.videoWidth
    canvas.height = inVideoRef.value.videoHeight
    context.drawImage(inVideoRef.value, 0, 0, canvas.width, canvas.height)
    // 将canvas内容转换为图片URL
    inImage.value = canvas.toDataURL('image/png')
  } else {
    ElMessage.error('Video is not ready or not available.')
  }
}

const submitInImage = async () => {
  if (!inImage.value) {
    ElMessage.error('图片不能为空')
    return
  }

  await billCarInService(inImage.value).then((response) => {
    ElMessage.success(response.message)
  })
}

const captureOutImage = () => {
  if (outVideoRef.value && outVideoRef.value.currentTime > 0) {
    const canvas = document.createElement('canvas')
    const context = canvas.getContext('2d')
    canvas.width = outVideoRef.value.videoWidth
    canvas.height = outVideoRef.value.videoHeight
    context.drawImage(outVideoRef.value, 0, 0, canvas.width, canvas.height)
    outImage.value = canvas.toDataURL('image/png')
  } else {
    ElMessage.error('Video is not ready or not available.')
  }
}

const submitOutImage = async () => {
  if (!outImage.value) {
    ElMessage.error('图片不能为空')
    return
  }

  await billCarOutService(outImage.value).then((response) => {
    ElMessage.success(response.message)
  })
}

// 在组件挂载时获取摄像头流
onMounted(async () => {
  await getCameraStream()
})

onUnmounted(() => {
  // 离开页面时停止摄像头流
  stream.value.getTracks().forEach((track) => track.stop())
})
</script>

<template>
  <el-container>
    <el-header>
      <h1>停车场管理系统</h1>
      <WelcomeDiv />
    </el-header>

    <el-main>
      <el-row>
        <el-col :span="12">
          <h2>车辆进入</h2>

          <video
            ref="inVideoRef"
            autoplay
            playsinline
            class="video-stream"
          ></video>

          <div>
            <el-button type="primary" @click="captureInImage">
              捕获车辆图片
            </el-button>
            <el-button type="primary" @click="submitInImage">
              放行车辆进入
            </el-button>
          </div>

          <div class="image-container">
            <img
              v-if="inImage"
              :src="inImage"
              alt="Captured Image"
              class="captured-image"
            />
          </div>
        </el-col>

        <el-col :span="12">
          <h2>车辆离开</h2>

          <video
            ref="outVideoRef"
            autoplay
            playsinline
            class="video-stream"
          ></video>

          <div>
            <el-button type="primary" @click="captureOutImage">
              捕获车辆图片
            </el-button>
            <el-button type="primary" @click="submitOutImage">
              放行车辆离开
            </el-button>
          </div>

          <div class="image-container">
            <img
              v-if="outImage"
              :src="outImage"
              alt="Captured Image"
              class="captured-image"
            />
          </div>
        </el-col>
      </el-row>
    </el-main>

    <el-footer> ©2024 Created by lt</el-footer>
  </el-container>
</template>

<style lang="scss" scoped>
.el-container {
  width: 100%;
  height: 100vh;
  /* 沾满视口高度 */
  background-color: #fff;

  .el-header {
    height: auto;
    background-color: var(--el-color-primary-light-8);
    margin-bottom: 10px;
    padding-bottom: 5px;

    h1 {
      text-align: center;
      font-size: 30px;
      color: #606266;
      height: 60px;
      line-height: 60px;
    }
  }

  .el-main {
    height: 100%;
    padding: 0;
    margin-bottom: 10px;

    .el-row {
      height: 100%;

      .el-col {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        align-items: center;
        height: 100%;
        width: 100%;
        // overflow: hidden; /* 隐藏超出容器的内容 */

        .video-stream {
          height: 35vh;
          /* 高度占视口的一半 */
          object-fit: cover;
          /* 保持视频比例填充容器 */
          // margin-bottom: 10px; /* 与按钮之间的间距 */
        }

        .image-container {
          height: 35vh;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .captured-image {
          height: 100%;
          object-fit: contain;
          /* 保持图片比例并包含在容器内 */
        }
      }
    }
  }

  .el-footer {
    background-color: #f2f3f5;
    font-size: 10px;
    text-align: center;
    height: 40px;
    line-height: 40px;
  }
}
</style>
